package com.zhang.controller;


import com.github.pagehelper.PageInfo;
import com.zhang.domain.*;
import com.zhang.query.DepartmentQuery;
import com.zhang.query.EmployeeQuery;
import com.zhang.service.IEmpService;
import com.zhang.util.AjaxResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author zh
 * @create 2022-02-14 9:06
 */

@RestController
@RequestMapping("employee")
public class EmployeeController {
    @Autowired
    private IEmpService service;
    @PutMapping
    private AjaxResult addOrUpdate(@RequestBody Manager m){
        if(m.getId()!=null){
            try {
                service.update(m);
                return AjaxResult.me();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.me(false,"修改失败");
            }
        }else {
            try {
                service.add(m);
                return AjaxResult.me();
            } catch (Exception e) {
                e.printStackTrace();
                return AjaxResult.me(false,"添加失败");
            }
        }
    }
    @DeleteMapping("/{id}")
    private AjaxResult delete(@PathVariable("id") Long id){
        try {
            service.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"删除失败");
        }
    }
    @GetMapping
    private AjaxResult getAll(){
        try {
            List<Manager> managers = service.selectAll();
            return AjaxResult.me(managers);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"查询失败");
        }
    }
    @PostMapping
    private AjaxResult getAllData(@RequestBody EmployeeQuery query){
        try {
            PageInfo<Manager> pageInfo = service.selectData(query);
            return AjaxResult.me(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"查询失败");
        }
    }
    @DeleteMapping("/deleteAll")
    private AjaxResult deleteAll(@RequestBody Long[] id){
        try {
            service.deleteAll(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"修改失败");
        }
    }
    @GetMapping("/selAllDept")
    private AjaxResult selAllDept(){
        try {
            List<Department> strings = service.selAllDept();
            return AjaxResult.me(strings);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"查询失败");
        }
    }
    @GetMapping("/selAllLoginInfo")
    private AjaxResult selAllLoginInfo(){
        try {
            List<LoginInfo> strings = service.selAllLoginInfo();
            return AjaxResult.me(strings);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"查询失败");
        }
    }
    @GetMapping("/selAllShop")
    private AjaxResult selAllShop(){
        try {
            List<Shop> strings = service.selAllShop();
            return AjaxResult.me(strings);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me(false,"查询失败");
        }
    }
}
